package com.daocao.hua.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.model.AlipayListModel;
import com.daocao.hua.view.ListItemView;

import java.util.List;

/**
 * Created by xsooy-pc on 18-7-31.
 */

public class AlipayAddListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<AlipayListModel.DataBean> data;
    private ListItemView lastOpenedSwipeLayout;
    public AlipayAddListAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public void updateData(List<AlipayListModel.DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public AlipayListModel.DataBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = inflater.inflate(R.layout.item_alipay_add, parent, false);

            holder = new Holder();
            holder.btnDelete = (TextView) convertView.findViewById(R.id.btn_delete);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title);
            holder.layoutTotal = (LinearLayout) convertView.findViewById(R.id.layout_total);
            holder.listItemView=(ListItemView) convertView.findViewById(R.id.listview);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        AlipayListModel.DataBean item = getItem(position);
        holder.txtTitle.setText(item.account);

        holder.listItemView.setOnSwipeListener(new ListItemView.OnSwipeListener() {
            @Override
            public void onOpen(ListItemView swipeLayout) {
                //当前 item 被打开时，记录下此 item
                lastOpenedSwipeLayout = swipeLayout;
            }

            @Override
            public void onClose(ListItemView swipeLayout) {
            }

            @Override
            public void onStartOpen(ListItemView swipeLayout) {
                //当前 item 将要打开时关闭上一次打开的 item
                if (lastOpenedSwipeLayout != null) {
                    lastOpenedSwipeLayout.close();
                }
            }

            @Override
            public void onStartClose(ListItemView swipeLayout) {
            }
        });

        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAlipayClick != null) {
                    onAlipayClick.onClick(item.account, position);
                }
            }
        });
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onAlipayClick != null) {
                    onAlipayClick.delete(item.account);
                }
            }
        });
        return convertView;
    }

    public interface OnAlipayClick {
        void onClick(String account, int poi);

        void delete(String account);
    }

    private OnAlipayClick onAlipayClick;

    public void setonAlipayClickListener(OnAlipayClick onAlipayClick) {
        this.onAlipayClick = onAlipayClick;
    }

    private final class Holder {
        TextView btnDelete;
        TextView txtTitle;
        LinearLayout layoutTotal;
        ListItemView listItemView;
    }
}
